package com.bnyy.medicalHousekeeper.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.WebChromeClient;
import com.just.agentweb.WebViewClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

public class WebViewActivity extends BaseActivityImpl implements BaseActivityImpl.UnnecessaryLogin, BaseActivityImpl.UnnecessaryRequestGlobalParams {

    @BindView(R.id.weblayout)
    LinearLayout weblayout;


    protected AgentWeb mAgentWeb;
    private List<String> mLoadHistoryUrls;
    private String mUrl;

    public static void show(Context context, String url) {
        Log.e("WebViewActivity", url);
        Intent starter = new Intent(context, WebViewActivity.class);
        starter.putExtra("url", url);
        context.startActivity(starter);
    }

    @Override
    public AppCompatActivity getSelfActivity() {
        return this;
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_web_view;
    }

    @Override
    public boolean unuseRefreshLayout() {
        return false;
    }

    @Override
    public String getTitleStr() {
        return " ";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUrl = getIntent().getStringExtra("url");
        if (TextUtils.isEmpty(mUrl)) {
            finish();
        }
        mLoadHistoryUrls = new ArrayList<>();

        LinearLayout parent = new LinearLayout(this);
        parent.setOrientation(LinearLayout.VERTICAL);
        parent.setBackgroundColor(Color.parseColor("#F9F9F9"));

        mAgentWeb = AgentWeb.with(this)//传入Activity
                .setAgentWebParent(weblayout, new LinearLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams
                .useDefaultIndicator()// 使用默认进度条
//                .setWebViewClient(mWebViewClient)
                .setWebChromeClient(new WebChromeClient(){
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        super.onReceivedTitle(view, title);
                        setTitleStr(title);
                    }
                })
//                .defaultProgressBarColor() // 使用默认进度条颜色
                .createAgentWeb()//
                .ready()
                .go(mUrl);

//        setOnBackClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (mUrl.equals(Constant.NetWork.URL_REGISTER)) {
//                    EventBus.getDefault().post(new EventBusData(Constant.EventBusTag.REGISTER));
//                }
//                finish();
//            }
//        });
    }

//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//        if (mUrl.equals(Constant.NetWork.URL_REGISTER)) {
//            EventBus.getDefault().post(new EventBusData(Constant.EventBusTag.REGISTER));
//        }
//        finish();
//    }

    protected WebViewClient mWebViewClient = new WebViewClient() {

        private HashMap<String, Long> timer = new HashMap<>();

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return shouldOverrideUrlLoading(view, request.getUrl().toString());
        }

        @Nullable
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            return super.shouldInterceptRequest(view, request);
        }

        //
        @Override
        public boolean shouldOverrideUrlLoading(final WebView view, String url) {
            view.loadUrl(url);
            mLoadHistoryUrls.add(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);

//			Log.i(TAG, "onReceivedHttpError:" + 3 + "  request:" + mGson.toJson(request) + "  errorResponse:" + mGson.toJson(errorResponse));
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);

//			Log.i(TAG, "onReceivedError:" + errorCode + "  description:" + description + "  errorResponse:" + failingUrl);
        }
    };
}
